﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace HAOest.Forms.Buttons
{
    /// <summary>
    /// 高级按钮，包含图片和文字
    /// </summary>
    public partial class RichButtonOld : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public RichButtonOld()
        {
            InitializeComponent();
        }

        private Image buttonPicture = Properties.Resources.AdvanceTools_MenuMgr;                //按钮图片
        private string buttonText = "按钮文字";               //按钮文字
        private bool focused = false;                       //焦点
        private State state = State.Normal;                 //状态



        /// <summary>
        /// 按钮图片
        /// </summary>
        [Browsable(true)]
        [Description("按钮图片")]
        [Category("外观")]
        public Image ButtonPicture
        {
            get
            {
                return buttonPicture;
            }
            set
            {
                buttonPicture = value;
                ButtonPictureBox.Image = buttonPicture;
            }
        }

        /// <summary>
        /// 按钮文字
        /// </summary>
        [Browsable(true)]
        [Description("按钮文字")]
        [Category("外观")]
        public string ButtonText
        {
            get
            {
                return buttonText;
            }
            set
            {
                buttonText = value;
                ButtonTextLabel.Text = buttonText;
            }
        }

        /// <summary>
        /// 重写控件焦点属性
        /// </summary>
        [Description("重写控件焦点属性")]
        public new bool Focused
        {
            get { return focused; }
            set
            {
                focused = value;

                if (focused)
                    state = State.Focused;
                else
                    state = State.LostFocused;

                Invalidate(false);
            }
        }

        /// <summary>
        /// 鼠标经过
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            state = State.MouseEnter;
            Invalidate(false);
        }

        /// <summary>
        /// 鼠标离开
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (!Focused)
            {
                state = State.LostFocused;
                Invalidate(false);
            }
        }

        /// <summary>
        /// 焦点进入
        /// </summary>
        /// <param name="e"></param>

        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            Focused = true;
        }

        /// <summary>
        /// 失去焦点
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            Focused = false;
        }

        /// <summary>
        /// 只响应单击鼠标左键事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClick(EventArgs e)
        {
            MouseEventArgs m = (MouseEventArgs)e;
            if (m.Button == MouseButtons.Left)
            {
                base.OnClick(e);
                Focus();
            }
        }

        /// <summary>
        /// 自定义绘制
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            switch (state)
            {
                case State.Focused:
                    this.BackgroundImage = Properties.Resources.rich_button_layer_opacity_50;
                    break;
                case State.MouseEnter:
                    if (!Focused)
                        this.BackgroundImage = Properties.Resources.rich_button_layer_opacity_20;
                    break;
                default:
                    this.BackgroundImage = null;
                    break;
            }
        }

        private void RichButton_MouseEnter(object sender, EventArgs e)
        {
            OnMouseEnter(e);
        }

        private void RichButton_MouseLeave(object sender, EventArgs e)
        {
            OnMouseLeave(e);
        }

    }
}
